<template>
  <div class="div">
    <div class="div1">
      <que :queList="queList" :butFlag="butFlag" :rules="rules" :items="items" :options="opti" @add="add"
           :addForms="addForms" @query="query" @shows="shows" @showss="showss"/>
    </div>
    <Tables :items="items" :butFlag="butFlag" :list="list" :loading="loading" :rules="rules" :high="high"
            :options="opti" @show="show" :editForm="editForm" @update="update" @del="del"/>
    <el-col :md="{span: 5, offset: 6}" :lg="{span: 4, offset: 10}">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[20, 30, 40, 50]"
        :page-size="pagesize"
        :current-page="page"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-col>
  </div>
</template>
<script>
  import Tables from '../components/Tables.vue'
  import axios from 'axios'
  import Que  from '../components/Que.vue'
  import moment from 'moment'
  import {mapState} from 'vuex'
  export default{
    name: 'Types',
    components: {
      Que,
      Tables
    },
    data () {
      return {
        list: [],
        high: 0,
        loading: false,
        butFlag: true,
        items: [
          {
            prop: 'title',
            label: '标题',
            add: true,
            table: true,
            edit: true,
            options: [],
            optio: '0',
            fixed: true
          },
          {
            prop: 'id',
            label: '编号',
            add: false,
            table: false,
            edit: false
          },
          {
            prop: 'createDate',
            label: '创建日期',
            add: false,
            table: false,
            edit: false,
//            width: '200',
            meth: function (row, column) {
              var date = row[column.property]
              if (date === null) {
                return date
              } else {
                return moment(date).format('YYYY-MM-DD HH:mm:ss')
              }
            },
            dateSelect: '0',
            fixed: false
          },
          {
            prop: 'modifyDate',
            label: '修改日期',
            add: false,
            table: true,
            edit: false,
//            width: '200',
            meth: function (row, column) {
              var date = row[column.property]
              if (date === null) {
                return date
              } else {
                return moment(date).format('YYYY-MM-DD HH:mm:ss')
              }
            },
            dateSelect: '0',
            fixed: false
          },
          {
            prop: 'imgUrl',
            label: '图片',
            add: true,
            table: true,
            edit: true,
            upload: '0',
            path: `${this.BASE_URL}/types/get`,
            fileList: 'fileList',
            img: '0',
            fixed: true
          },
          {
            prop: 'url',
            label: '预览地址',
            table: true,
          },
          {
            prop: 'status',
            label: '状态',
            add: true,
            table: true,
            edit: true,
            meth: function (row, column) {
              var state = row[column.property]
              if (state === false) {
                state = '下线'
              } else {
                state = '上线'
              }
              return state
            },
            options: [{
              value: false,
              label: '下线'
            },
              {
                value: true,
                label: '上线'
              }],
            fixed: false
          },
          {
            prop: 'types',
            label: '类型',
            add: true,
            table: true,
            edit: true,
            meth: function (row, column) {
              var state = row[column.property]
              if (state === '01') {
                state = 'banner图'
              } else if (state === '02') {
                state = '公告'
              } else if (state === '88') {
                state = '常见问题'
              } else if (state === '03') {
                state = '读者需知'
              }
              return state
            },
            options: [{
              value: '01',
              label: 'banner图'
            },
              {
                value: '02',
                label: '公告'
              },
              {
                value: '88',
                label: '常见问题'
              },
              {
                value: '03',
                label: '读者需知'
              }],
            fixed: false
          },
          {
            prop: 'device',
            label: '设备',
            add: true,
            table: true,
            edit: true,
            meth: function (row, column) {
              var state = row[column.property]
              if (state === false) {
                state = '微信'
              } else {
                state = 'app'
              }
              return state
            },
            options: [{
              value: false,
              label: '微信'
            },
              {
                value: true,
                label: 'app'
              }],
            fixed: true
          },
          {
            prop: 'url',
            label: '跳转链接',
            add: false,
            table: false,
            edit: false
          }
        ],
        rules: {
          imgUrl: [
            {required: true, message: '请选择文件', trigger: 'blur'}
          ],
          title: [
            {required: true, message: '请填写标题', trigger: 'blur'}
          ],
          status: [
            {type: 'boolean', required: true, message: '请选择状态', trigger: 'blur'}
          ],
          types: [
            {type: 'string', required: true, message: '请选择类型', trigger: 'blur'}
          ],
          device: [
            {type: 'boolean', required: true, message: '请选择显示设备类型', trigger: 'blur'}
          ]
        },
        queList: [
          {
            width: '3',
            name: '标题',
            prop: 'title',
            i1: '0'
          },
          {
            width: '3',
            name: '类型',
            prop: 'types',
            i3: [{
              value: '01',
              label: 'banner图'
            },
              {
                value: '02',
                label: '公告'
              },
              {
                value: '88',
                label: '常见问题'
              },
              {
                value: '03',
                label: '读者需知'
              }]
          },
          {
            width: '3',
            name: '状态',
            prop: 'status',
            i3: [{
              value: false,
              label: '下线'
            },
              {
                value: true,
                label: '上线'
              }]
          }],
        page: 1,
        pagesize: 20,
        total: 0,
        opti: [],
        title: '',
        status: '',
        types: '',
        editForm: {
          status: '',
          title: '',
          imgUrl: '',
          fileList: [],
          id: '',
          types: '',
          device: ''
        },
        addForms: false
        // querys: {
        //   title: '',
        //   types: '',
        //   status: ''
        // }
      }
    },
    mounted () {
      axios({
        method: 'get',
        url: `${this.BASE_URL}/news/getAlls`,
        responseType: 'json',
      }).then((response) => {
        var list = []
        response.data.data.forEach((item) => {
          const map = new Map()
          map.set("label", item.title)
          map.set("value", item.id)
          list.push(map)
        })
        this.opti = list
      }).catch(() => {
        this.loading = false
      })
      this.get()
      this.high = window.innerHeight - 175
    },
    computed: {
      ...mapState({
        isLogin: state => state.eui.isLogin,
        username: state => state.eui.username
      })
    },
    created () {
      if (!sessionStorage.getItem('username')) {
        this.$router.replace('/')
      }
    },
    methods: {
      handleCurrentChange (val) {
        this.page = val
        this.get()
      },
      handleSizeChange (val) {
        this.pagesize = val
        this.get()
      },
      add (msg) {
        axios({
          method: 'get',
          url: `${this.BASE_URL}/types/add`,
          responseType: 'json',
          params: {
            page: this.page,
            pagesize: this.pagesize,
            imgUrl: msg.imgUrl,
            title: msg.title,
            status: msg.status,
            types: msg.types,
            device: msg.device
          }
        }).then((response) => {
          this.addForms = false
          this.loading = false
          this.total = response.data.total
          this.list = response.data.data
        }).catch(() => {
          this.loading = false
        })
      },
      show (msg) {
        this.editForm.status = msg.status
        this.editForm.title = msg.title
        this.editForm.imgUrl = msg.imgUrl
        this.editForm.fileList = [{name: 'a', url: msg.imgUrl}]
        this.editForm.id = msg.id
        this.editForm.types = msg.types
        this.editForm.device = msg.device
      },
      update (msg) {
        this.loading = true
        axios({
          method: 'get',
          url: `${this.BASE_URL}/types/refresh`,
          responseType: 'json',
          params: {
            id: msg.id,
            imgUrl: msg.imgUrl,
            title: msg.title,
            status: msg.status,
            types: msg.types,
            device: msg.device
          }
        }).then((response) => {
          if (response.data.statuCode === '0') {
            this.loading = false
            this.total = response.data.total
            this.list = response.data.data
            this.$message({
              showClose: true,
              message: '修改成功',
              type: 'success'
            })
          }
        }).catch(() => {
          this.loading = false
        })
      },
      del (msg) {
        this.loading = true
        axios({
          method: 'get',
          url: `${this.BASE_URL}/types/del`,
          responseType: 'json',
          params: {
            page: this.page,
            pagesize: this.pagesize,
            id: msg.id
          }
        }).then((response) => {
          this.loading = false
          this.total = response.data.total
          this.list = response.data.data
        }).catch(() => {
          this.loading = false
        })
      },
      query (msg) {
        this.title = msg.title
        this.status = msg.status
        this.types = msg.types
        this.page = 1
        this.get()
      },
      get () {
        this.loading = true
        axios({
          method: 'get',
          url: `${this.BASE_URL}/types/getAll`,
          responseType: 'json',
          params: {
            page: this.page,
            pagesize: this.pagesize,
            title: this.title,
            status: this.status,
            types: this.types
          }
        }).then((response) => {
          this.list = []
          if (response.data.statuCode === '0') {
            this.total = response.data.total
            response.data.data.forEach((item) => {
              this.list.push(item)
              this.loading = false
            })
          } else {
            this.loading = false
            this.total = response.data.total
            // this.$alert('没有相关内容', '提示', {
            //   confirmButtonText: '确定'
            // })
          }
        }).catch(() => {
          this.loading = false
        })
      },
      shows (msg) {
        this.addForms = true
      },
      showss (msg) {
        this.addForms = false
      }
    }
  }
</script>
<style scoped>
  .div {
    /*margin-top: 10px;*/
  }

  .div1 {
    width: 100%;
    height: 50px;
    padding-top: 10px;
  }

  .el-select-dropdown__item {
    height: auto;
  }
</style>
